<?php
class axslt {

	public function transform($xml,$xsl) {
		$doc = new DOMDocument();
		$sty = new DOMDocument();
		if(is_array($xml)) {
			$xml = $this->array2xml($xml);
		}
		if(is_array($xsl)) {
			$xsl = $this->array2xml($xsl);
		}
		if(is_file($xml)){
			$xml = file_get_contents($xml);
		} 
		if (is_file($xsl)) {
			$xsl = file_get_contents($xsl);
		}
		 

		if(substr($xml,0,5)!="<?xml"){$xml = "<?xml version='1.0' encoding='UTF-8'?>".$xml;}
		if(substr($xsl,0,5)!="<?xml"){$xsl = "<?xml version='1.0' encoding='UTF-8'?>".$xsl;}
		file_put_contents("/tmp/trans.xml", $xml);
		$doc->loadXML($xml);
		$sty->loadXML($xsl);		
		$xslt = new XSLTProcessor();
		$xslt->importStylesheet($sty);
		$rxml = $xslt->transformToDoc($doc);
		$xml = $rxml->saveHTML();
		if(substr($xml,0,5)!="<?xml"){$xml = "<?xml version='1.0' encoding='UTF-8'?>".$xml;}

		echo $xml;
	}
	
	function array2xml($xml,$pt=null){
		if($pt!=null) {$cab = "<".$pt.">";} else {$cab = "<?xml version='1.0' encoding='UTF-8'?>";}
		if(is_array($xml)) {
			foreach($xml as $subkey => $subxml) {
				(gettype($subkey) != 'integer')?$npt=$subkey:$npt=$pt;
				$cp .= $this->array2xml($subxml, $subkey);
			}
		} else {
			$cp .= $xml;
		}
		if($pt!=null) {$ft .= "</".$pt.">";}
		return $cab.$cp.$ft;
	}
} 
?>